layui.define('jquery', function(exports){
  "use strict";
  
  var $ = layui.jquery;
  var THIS = 'layui-this', HIDE = 'layui-hide';

  var Select = function(){
    var TIPS = '请选择', CLASS = 'layui-form-select', TITLE = 'layui-select-title'
      ,NONE = 'layui-select-none', initValue = '', thatInput
      ,selects = $(".layui-select");

      //检测值是否不属于select项
      var notOption = function(value, callback, origin,dds){
        var num = 0;   
        layui.each(dds, function(){
          var othis = $(this)
          ,text = othis.text()
          ,not = text.indexOf(value) === -1;
          if(value === '' || (origin === 'blur') ? value !== text : not) num++;
          origin === 'keyup' && othis[not ? 'addClass' : 'removeClass'](HIDE);
        });
        var none = num === dds.length;
        return callback(none), none;
      };


    //点开下拉
    var showDown = function(reElem,dds){
      reElem.addClass(CLASS+'ed'); 
      dds.removeClass(HIDE);
    }, hideDown = function(reElem,input,dl, dds){
      reElem.removeClass(CLASS+'ed');
      input.blur();
      notOption(input.val(), function(none){
        if(none){
          initValue = dl.find('.'+THIS).html();
          input && input.val(initValue);
        }
      },'', dds);

    };

    selects.each(function(index, select){
      var othis = $(this), hasRender = othis.next('.'+CLASS), disabled = this.disabled;
      var value = select.value, selected = $(select.options[select.selectedIndex])
      
      if(typeof othis.attr('lay-ignore') === 'string') return othis.show();
      
      var isSearch = typeof othis.attr('lay-search') === 'string';

      //替代元素
      var reElem = $(['<div class="layui-unselect '+ CLASS + (disabled ? ' layui-select-disabled' : '') +'">'
        ,'<div class="'+ TITLE +'"><input type="text" placeholder="'+ (select.options[0].innerHTML ? select.options[0].innerHTML : TIPS) +'" value="'+ (value ? selected.html() : '') +'" '+ (isSearch ? '' : 'readonly') +' class="layui-input layui-unselect'+ (disabled ? (' '+DISABLED) : '') +'">'
        ,'<i class="layui-edge"></i></div>'
        ,'<dl class="layui-anim layui-anim-upbit'+ (othis.find('optgroup')[0] ? ' layui-select-group' : '') +'">'+ function(options){
          var arr = [];
          layui.each(options, function(index, item){
            if(index === 0 && !item.value) return;
            if(item.tagName.toLowerCase() === 'optgroup'){
              arr.push('<dt>'+ item.label +'</dt>'); 
            } else {
              arr.push('<dd lay-value="'+ item.value +'" class="'+ (value === item.value ?  THIS : '') + (item.disabled ? (' '+DISABLED) : '') +'">'+ item.innerHTML +'</dd>');
            }
          });
          return arr.join('');
        }(othis.find('*')) +'</dl>'
      ,'</div>'].join(''));
      
      hasRender[0] && hasRender.remove(); //如果已经渲染，则Rerender
      othis.after(reElem);
      $(this).hide();

      //选择展开
      var dl = $(reElem).find("dl"),dds = $(dl).find("dd"),input = $(reElem).find("input"),title = reElem.find('.' + TITLE);
      $(reElem).on("click",function(){
        if( $(reElem).hasClass('layui-form-selected')){
          hideDown(reElem, input,dl ,dds);
        }else{
          showDown(reElem,dds);
        }        
      });

      //选中
      dds.on('click', function(){
        var othis = $(this), value = othis.attr('lay-value');
        var filter = select.attr('lay-filter'); //获取过滤器

        if(othis.hasClass(DISABLED)) return false;
        
        select.val(value).removeClass('layui-form-danger'), input.val(othis.text());
        othis.addClass(THIS).siblings().removeClass(THIS);
        layui.event.call(this, MOD_NAME, 'select('+ filter +')', {
          elem: select[0]
          ,value: value
          ,othis: reElem
        });

        hideDown(reElem, input,dl ,dds);
        
        return false;
      });


    });
  };

  Select.prototype.render = function(){
    // layui_select.
  }


  var select = new Select()
  select.render();
});